//
//  AGLTopicCollectionCell.m
//  滴滴快拍
//
//  Created by dllo on 16/5/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AGLTopicCollectionCell.h"
#import "AFNetworking.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height


@implementation AGLTopicCollectionCell


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
        
    }
    
    return self;
}

- (void)setTopic:(AGLTopic *)topic {
    if (_topic != topic) {
        _topic = topic;
        [self createData];

    }
}

- (void)createView {
    self.imgView = [[BaseImageView alloc] init];
    [self.contentView addSubview:self.imgView];
    
    self.topicLabel = [[BaseLabel alloc] init];
    [self.contentView addSubview:self.topicLabel];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imgView.frame = self.contentView.frame;
    
    self.topicLabel.frame = CGRectMake(0, HEIGHT/5*4, WIDTH, HEIGHT/5);
}

- (void)createData {
    
    [self.imgView setImageWithURL:[NSURL URLWithString:self.topic.img] placeholderImage:[UIImage imageNamed:@"PlaceHolder"]];
    self.topicLabel.text = self.topic.title;
    
    self.topicLabel.textColor = [UIColor whiteColor];
    self.topicLabel.textAlignment = 1;
    
    // 设置颜色
    NSString *cString = self.topic.color;
    cString = [cString substringFromIndex:1];
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    //r
    NSString *rString = [cString substringWithRange:range];
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    self.topicLabel.backgroundColor = [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:0.8];

    
    
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
